/**
 * Copyright (c) 2018 小润科技 All rights reserved.
 * <p>
 * 版权所有，侵权必究！
 */

package com.xr.sy.modules.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xr.common.annotation.DataFilter;
import com.xr.common.constant.Constant;
import com.xr.common.page.PageData;
import com.xr.common.service.impl.BaseServiceImpl;
import com.xr.common.service.impl.CrudServiceImpl;
import com.xr.common.utils.ConvertUtils;
import com.xr.sy.modules.business.dao.QuestionDao;
import com.xr.sy.modules.business.dto.NoticeDTO;
import com.xr.sy.modules.business.dto.QuestionDTO;
import com.xr.sy.modules.business.entity.QuestionEntity;
import com.xr.sy.modules.business.entity.SysMessageEntity;
import com.xr.sy.modules.business.service.QuestionService;
import com.xr.sy.modules.message.dto.SysSmsDTO;
import com.xr.sy.modules.message.entity.SysSmsEntity;
import com.xr.sy.modules.security.user.SecurityUser;
import com.xr.sy.modules.security.user.UserDetail;
import com.xr.sy.modules.sys.service.SysDeptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class QuestionServiceImpl extends CrudServiceImpl<QuestionDao, QuestionEntity, QuestionDTO> implements QuestionService {
    @Autowired
    private SysDeptService sysDeptService;

    @Override
    public QueryWrapper<QuestionEntity> getWrapper(Map<String, Object> params) {
        String title = (String) params.get("title");

        QueryWrapper<QuestionEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(title), "title", title);

        UserDetail u = SecurityUser.getUser();
        if(null != u.getDeptId()){
            List<Long> deptIds = sysDeptService.getSubDeptIdList(u.getDeptId());
            wrapper.in("dept_id", deptIds);
        }

        wrapper.orderByDesc("pub_date");

        return wrapper;
    }

    @Override
    public PageData<QuestionDTO> page(Map<String, Object> params) {
        IPage<QuestionEntity> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),
                getWrapper(params)
        );
        return getPageData(page, QuestionDTO.class);
    }

    @Override
    public QuestionDTO latest() {
        //查询
        QuestionEntity entity = baseDao.latest();
        return ConvertUtils.sourceToTarget(entity, QuestionDTO.class);
    }

    @Override
    public QuestionDTO get(Long id) {
        QuestionEntity entity = baseDao.selectById(id);
        return ConvertUtils.sourceToTarget(entity, QuestionDTO.class);
    }

    @Override
    public void save(QuestionDTO dto) {
        QuestionEntity entity = ConvertUtils.sourceToTarget(dto, QuestionEntity.class);

        insert(entity);
    }

    @Override
    public void update(QuestionDTO dto) {
        QuestionEntity entity = ConvertUtils.sourceToTarget(dto, QuestionEntity.class);

        updateById(entity);
    }

}
